<template>
    <div>
    <div class="table-search">
        <el-row>
            <el-col>
                <el-input
                        placeholder="输入状态码"
                        clearable
                        v-model="listQuery.code">
                </el-input>
                <el-input
                        placeholder="输入状态信息"
                        clearable
                        v-model="listQuery.message">
                </el-input>
                <el-button type="primary" @click="handleSearch" icon="el-icon-search" >搜索</el-button>
                <el-button type="primary"  icon="plus" @click="add">添加</el-button>
            </el-col>
        </el-row>
    </div>
    <div class="table-container">
        <el-table
        ref="msgTable"
        :data="tableDataMsg"
        row-key="id"
        border
        highlight-current-row
        @row-click="RowClick"
        v-bind:class="{'is-edit': isEdit}"
        style="width: 100%">

        <el-table-column align="center" label="序号" width ="100" >
            <template slot-scope="scope">
                <span >{{ scope.$index+1 }}</span>
            </template>
            </el-table-column>
        <el-table-column
        align="center"
        label="状态码">
        <template slot-scope="scope">
            <el-input v-model="scope.row.code"></el-input>
            <span>{{ scope.row.code }}</span>
        </template>
        </el-table-column>
        <el-table-column
        align="center"
        label="状态信息">
        <template slot-scope="scope">
                <el-input  v-model="scope.row.message"></el-input>
                <span >{{ scope.row.message }}</span>
            </template>
        </el-table-column>
        <el-table-column
            align="center"
            label="是否可用"
            width="">
            <template slot-scope="scope">
            <span >{{scope.row.enable}}</span>
            <el-select  v-model="scope.row.enable">
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.codeKey"
                :value="item.codeKey">
            </el-option>
            </el-select>
            </template>
        </el-table-column>
        <el-table-column
            align="center"
            width="200px"
            label="操作"
            >
        <template slot-scope="scope">
         <div class="default">
          <el-button type="text"  @click="edit(scope)">修改</el-button>
          <el-button type="text" @click="remove(scope)">删除</el-button>
       </div>
       <div class="confirm">
         <el-button type="text" @click="submit(scope)">确认</el-button>
          <el-button type="text" @click="cancle(scope)">取消</el-button>
       </div>
        </template>
        </el-table-column>
    </el-table>
    <div class="table-page">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next ,jumper"
        :total="total">
        </el-pagination>
    </div>
    </div>
</div>
</template>
<script>
import { getList, update, insert } from '@/api/sys/message'
import { getDetail } from '@/api/sys/detail'
export default {
    data() {
        return {
            tableDataMsg: [],
            options: [
                    {id:1,label:"是",value:true},
                    {id:2,label:"否",value:false}
            ],
            tempRow: {},
            total: null,
            adding: false,
            isEdit: false,
            listQuery: {
                currentPage: 1,
                size: 10,
                code: '',
                message: ''
            }
        }
    },
    created(){
        this.getList()
    },
    methods: {
        getList() {
            const params = this.listQuery
            getList(params).then((response) => {
                if (response.code === 0) {
                    this.tableDataMsg = response.data.records
                    this.total = response.data.total
                    this.isEdit = false
                    this.adding = false
                }
            })
        },
        handleSearch(){
            this.tableDataDetail = []
            this.listQuery.currentPage = 1
            this.getList()
        },
        handleSizeChange(val) {
            this.listQuery.currentPage = 1
            this.listQuery.size = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.listQuery.currentPage = val
            this.getList()
        },
        RowClick(row, eve ,column) {
            if(row.id === "") return
            if(row.id !== this.tempRow.id) {
                this.isEdit = false
            }
            if (this.adding) {
                this.$nextTick(function () {
                    this.tableDataMsg.splice(0,1)
                })
                this.adding = false
            }
        },
        add() {
            if(this.adding) return
            const newRow = {id:"", code:"", message: "",enable:true}
            this.tableDataMsg.unshift(newRow)
            this.recoveryHighLight(newRow)
            this.isEdit = true
            this.adding = true
        },
        edit(scope) {
            this.isEdit = true
            this.tempRow = Object.assign({}, this.tempRow, scope.row)
        },
        submit(scope) {
            const row = scope.row
            if (row.id === "") {
                insert(row).then((response) => {
                    if (response.code === 0) {
                        this.isEdit = false
                        this.adding = false
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: response.msg
                        })
                    }else{
                         this.$message({
                            type: 'error',
                            message: response.msg
                        })
                    }
                })
            } else {
                update(row).then((response) => {
                    if (response.code === 0) {
                        this.isEdit = false
                        this.getList()
                        this.$message({
                            type: 'success',
                            message: response.msg
                        })
                    }else{
                         this.$message({
                            type: 'error',
                            message: response.msg
                        })
                    }
                })
            }

        },
        cancle(scope) {
            const row = scope.row
            if (row.id === "") {
                this.getList()
                this.adding = false
            }
            this.isEdit = false
        },
        remove(scope) {
            const row = scope.row
            row.delFlag = 1
            this.$confirm('此操作将删除该行数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    update(row).then((response) => {
                        if(response.code === 0) {
                            this.getList()
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        recoveryHighLight(row){
            this.$nextTick(function () {
            this.$refs.msgTable.setCurrentRow(row)
            })
        }
    }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.table-container{
    margin: 10px
}
.table-container .el-input,.el-select,.confirm{
    display: none;
}
.is-edit .current-row .default {
    display: none
}
.is-edit .current-row span {
    display: none
}
.is-edit .current-row .confirm{
    display: block;
}
.is-edit .current-row .el-select {
    display: block
}
.is-edit .current-row .el-input {
    display: block
}
.table-search{
  margin: 10px;
  .el-input{
    width: 200px;
    margin-right: 6px;
  }
}
.table-page{
    margin-top: 10px
}


</style>

